﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Vkontakte.Api;
using Timer = System.Timers.Timer;

namespace VkClientIM.Windows
{
	/// <summary>
	/// Interaction logic for AuthenticationWindow.xaml
	/// </summary>
	public partial class AuthenticationWindow : Window
	{
		internal const int MyAppID = 1912632;
		private bool _needReCheck = false;
		private bool _isAuthenticated = false;

		public AuthenticationWindow()
		{
			InitializeComponent();
			//authorizator.Opacity = 0;
		}

		private void Close(bool result)
		{
			DialogResult = result;
		}

		private void SetResultEndExit()
		{
			if (Dispatcher.CheckAccess())
			{
				this.Close(_isAuthenticated);
			}
			else
			{
				this.Dispatcher.Invoke(new Action<bool>(this.Close), _isAuthenticated);
			}
		}

		private void AuthorizatorWinForms_Authenticated()
		{
			if (!_needReCheck)
			{
				_isAuthenticated = true;
				SetResultEndExit();
			}
		}

		private void authorizator_LoginCanceled()
		{
			_isAuthenticated = false;
			SetResultEndExit();
		}


		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			authorizator.ApplicationAccess = ApplicationAccess.AllExceptAddLeft;
			authorizator.AppID = MyAppID;
			authorizator.Authenticate();
		}

		public bool IsAuthenticated
		{
			get; private set;
		}

		
	}
}
